package de.mfits.fireflyplayer.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

public class PlaybackService implements ServiceConnection, IPlaybackService {
  private IPlaybackService service;
  private Runnable onReadyCallback;

  @Override
  public boolean play(int id) throws RemoteException {
    return service.play(id);
  }

  @Override
  public void start() throws RemoteException {
    service.start();
  }

  @Override
  public void stop() throws RemoteException {
    service.stop();
  }

  @Override
  public void pause() throws RemoteException {
    service.pause();
  }

  @Override
  public boolean isPlaying() throws RemoteException {
    return service.isPlaying();
  }

  @Override
  public void seekTo(int pos) throws RemoteException {
    service.seekTo(pos);
  }

  @Override
  public int getCurrentPosition() throws RemoteException {
    return service.getCurrentPosition();
  }

  @Override
  public int getDuration() throws RemoteException {
    return service.getDuration();
  }

  @Override
  public void setLooping(boolean looping) throws RemoteException {
    service.setLooping(looping);
  }

  @Override
  public void setVolume(float leftVolume, float rightVolume) throws RemoteException {
    service.setVolume(leftVolume, rightVolume);
  }

  @Override
  public void onServiceConnected(ComponentName name, IBinder binder) {
    service = IPlaybackService.Stub.asInterface(binder);
    if (onReadyCallback != null) {
      onReadyCallback.run();
    }
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
    service = null;
  }

  @Override
  public IBinder asBinder() {
    return service.asBinder();
  }

  public static PlaybackService bindClient(Context context) {
    Intent intent = new Intent(context, PlaybackServiceImpl.class);
    PlaybackService conn = new PlaybackService();
    context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    return conn;
  }

  public static IBinder bindService(final PlaybackServiceImpl s) {
    return new IPlaybackService.Stub() {
      @Override
      public boolean play(int id) {
        return s.play(id);
      }

      @Override
      public void start() throws RemoteException {
        s.start();
      }

      @Override
      public void stop() {
        s.stop();
      }

      @Override
      public void pause() throws RemoteException {
        s.pause();
      }

      @Override
      public boolean isPlaying() throws RemoteException {
        return s.isPlaying();
      }

      @Override
      public void seekTo(int pos) throws RemoteException {
        s.seekTo(pos);
      }

      @Override
      public int getCurrentPosition() {
        return s.getCurrentPosition();
      }

      @Override
      public int getDuration() {
        return s.getDuration();
      }

      @Override
      public void setLooping(boolean looping) throws RemoteException {
        s.setLooping(looping);
      }

      @Override
      public void setVolume(float leftVolume, float rightVolume) throws RemoteException {
        s.setVolume(leftVolume, rightVolume);
      }
    };
  }

  public void onReady(Runnable runnable) {
    if (service != null) {
      runnable.run();
    } else {
      onReadyCallback = runnable;
    }
  }
}
